from loaddata import mgDB
from collections import Counter
import datetime
'''
统计最大间隔
'''
def tj(dx):
    # 查看一下多少次中奖一次，间隔多少次
    r = []
    a = 0
    for d in dx:
        if d == 0:
            a += 1
        else:
            r.append(a)
            a = 0
    dxres = Counter(dx)
    mzl = round(dxres.get(1)/len(dx),4)


    print("统计：", dxres,"命中率：",mzl)
    print("中奖记录：", dx)
    print("最大间隔：", max(r))
    res = Counter(r)
    # 计算平均间隔
    print("平均间隔：", round(sum(r)/len(r),2))
    print("统计：", res)
    print("间隔多久中：", r)

def tongji(ds):
    dx = []
    ps = []
    for d in ds:
        z = 0
        if d.get("zhong") == 1:
            z = 1
            ps.append(d.get("ps"))
        dx.append(z)
    print(len(ds),sum(ps))
    tj(dx)

# 选择0:0的比赛
def selectds00():
    year = 2022  # 想要查询的年份
    start_of_year = datetime.datetime(year=year, month=1, day=1)
    end_of_year = start_of_year + datetime.timedelta(days=365) - datetime.timedelta(microseconds=1)
    query = {'matchTime': {'$gte': "2018-01-01", '$lt': "2018-03-01"}}

    ds = mgDB.get_jincai(query)
    selectds = []
    for ddd in ds:
        # 筛选一下
        a = ddd.get("a")     # 负
        h = ddd.get("h")     # 胜
        d = ddd.get("d")
        if a and h:
            # 找到最小
            aa = float(a)
            dd = float(d)
            hh = float(h)
            mm = aa
            if dd < mm:
                mm = dd
            if hh < mm:
                mm = hh

            if 2.0 < mm < 2.8:
                tt = {"zhong":0,"ps":0}
                sectionsNo999 = ddd.get("sectionsNo999")
                if "0:0"==sectionsNo999:
                    tt["zhong"] = 1
                    fixedBonus = ddd.get("fixedBonus")
                    matchResultList = fixedBonus.get("matchResultList")
                    if len(matchResultList) > 0:
                        for m in matchResultList:
                            if "CRS" == m.get("code"):# 比分
                                tt["ps"] += float(m.get("odds"))
                        # if "TTG"==m.get("code"):# 总进球
                        #     tt["ps"] += float(m.get("odds"))
                selectds.append(tt)

    return selectds


if __name__ == '__main__':
    selectds = selectds00()
    tongji(selectds)